package com.lzs.bootsafety.common.oss;

import com.UpYun;
import com.lzs.bootsafety.business.oss.entity.SysFile;
import com.lzs.bootsafety.common.oss.Exception.OssException;
import com.lzs.bootsafety.common.util.oss.FileUtils;
import com.qiniu.util.StringUtils;
import com.upyun.RestManager;
import com.upyun.UpException;
import com.upyun.UpYunUtils;
import okhttp3.Response;

import java.io.*;
import java.util.Date;
/**
 * @author  zhanshi Liu
 * @date    2020/04/22
 * @website www.jhmsona.cn
 */
public class YouPaiApi extends BaseApi {
    private String ypBucket;
    private String ypBucketDomain;
    private String ypPassword;
    private String ypUserName;
    private String ypStoragePath;
    private UpYun upYun;
    YouPaiApi() {
        super("又拍云");
    }
    public YouPaiApi init(String ypBucket,String ypBucketDomain,String ypPassword,String ypUserName,String ypStoragePath){
        this.ypBucket=ypBucket;
        this.ypBucketDomain=ypBucketDomain;
        this.ypPassword=ypPassword;
        this.ypUserName=ypUserName;
        this.ypStoragePath=ypStoragePath;
        upYun = new UpYun(ypBucket,ypUserName,ypPassword);
        upYun.setApiDomain(UpYun.ED_AUTO);
        upYun.setTimeout(60);
        return this;
    }
    @Override
    protected void check() {
        if (StringUtils.isNullOrEmpty(this.ypBucket) || StringUtils.isNullOrEmpty(this.ypUserName) || StringUtils.isNullOrEmpty(this.ypPassword)) {
            throw new OssException("[" + this.storageType + "]尚未配置又拍云，文件上传功能暂时不可用！");
        }
    }

    /**
     * RestManager.ED_AUTO  智能线路（推荐）：v0.api.upyun.com
     * RestManager.ED_TELECOM 电信线路：v1.api.upyun.com
     * RestManager.ED_CNC 联通（网通）线路：v2.api.upyun.com
     * RestManager.ED_CTT 移动（铁通）线路：v3.api.upyun.com
     */

    @Override
    public SysFile uploadFile(InputStream is, String fileUrl) {
        Date date = new Date();
        this.check();
        this.createNewName(fileUrl);
        String filePath = this.ypStoragePath+newFileName;
        try {
            String MD5 = UpYunUtils.md5(new byte[is.available()]);
            //upYun.setContentMD5(MD5);
            upYun.writeFile(filePath,is,true,null);
            return new SysFile()
                    .setSuffix(this.suffix)
                    .setFileHash(MD5)
                    .setUploadStartTime(date)
                    .setUploadEndTime(new Date())
                    .setFilePath(this.ypStoragePath+this.newFileName)
                    .setFullFilePath(this.ypBucketDomain+filePath);
        } catch (IOException e) {
            throw new OssException("["+this.storageType+"]IO异常"+e.getMessage());
        } catch (UpException e) {
            throw new OssException("["+this.storageType+"]上传失败:"+e.getMessage());
        }
    }

    @Override
    public boolean download(String fileName){
        check();
        try {
            File file = File.createTempFile("upyunTempFile_", "");
            upYun.readFile(fileName,file);
            FileUtils.downloadFile(fileName,new FileInputStream(file),storageType);
            return true;
        } catch (IOException e) {
            throw new OssException("["+this.storageType+"]下载IO异常"+e.getMessage());
        } catch (UpException e) {
            throw new OssException("["+this.storageType+"]下载失败:"+e.getMessage());
        }
    }

    @Override
    public boolean delete(String filePath) {
        check();
        try {
            boolean res = upYun.deleteFile(filePath,null);
            return res;
        } catch (IOException e) {
            throw new OssException("["+this.storageType+"]删除IO异常"+e.getMessage());
        } catch (UpException e) {
            throw new OssException("["+this.storageType+"]删除失败:"+e.getMessage());
        }

    }
}
